<template>
  <div class="control">
    <module
      ref="module"
      :moduleType="visitorRecordMonth.moduleType"
      :borderConfig="visitorRecordMonth.borderConfig"
      :moduleConfig="visitorRecordMonth.moduleConfig"></module>
    <module
      ref="module1"
      :moduleType="phoneOpenMonth.moduleType"
      :borderConfig="phoneOpenMonth.borderConfig"
      :moduleConfig="phoneOpenMonth.moduleConfig"></module>
    <module
      ref="module2"
      :borderConfig="patrolMonth.borderConfig"
      :moduleConfig="patrolMonth.moduleConfig"></module>
    <module
      ref="module3"
      :borderConfig="patrolOrgan.borderConfig"
      :isShowDimension="patrolOrgan.isShowDimension"
      :moduleConfig="patrolOrgan.moduleConfig"></module>
    <module
      ref="module4"
      :borderConfig="patrolSettingOrgan.borderConfig"
      :isShowDimension="patrolSettingOrgan.isShowDimension"
      :moduleConfig="patrolSettingOrgan.moduleConfig"></module>
  </div>
</template>

<script>
import module from '../components/SecondaryModule'
import { changeBarData, separateData } from './../../utils/index'
export default {
  components: {
    module
  },
  computed: {
    organId () {
      return this.$store.state.organId
    }
  },
  name: 'control',
  data () {
    return {
      // 访客数量趋势配置
      visitorRecordMonth: {
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '访客数量趋势',
          ref: 'visitorRecordMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: 'w',
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '万元',
              isNamePreposition: false,
              tipsMessage: '访客记录'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 手机开门趋势
      phoneOpenMonth: {
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '手机开门趋势',
          ref: 'phoneOpenMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: 'w',
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '万元',
              isNamePreposition: false,
              tipsMessage: '手机开门',
              tipsColor: ['#2e92cb', '#f29f46']
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 2 }]
        }
      },
      // 巡更指标趋势配置
      patrolMonth: {
        borderConfig: {
          titleText: '巡更指标趋势',
          moduleType: '4x1',
          optionsDirection: 'level',
          ref: 'patrolMonth'
        },
        moduleConfig: [
          {
            optionsConfig: {
              title: {
                text: '巡更完成率',
                top: 10,
                left: '50'
              },
              axis: {
                yAxisConversion: '%', // Y轴坐标%
                xAxisDataType: 2 // X轴坐标从1月份开始
              },
              yAxis: {
                max: 100
              },
              tooltipsConfig: {
                symbol: '%',
                isNamePreposition: false,
                tipsMessage: '巡更完成率'
              },
              grid: {
                top: 40
              },
              seriesData: []
            },
            legendDirection: 'level',
            legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
          },
          {
            optionsConfig: {
              title: {
                text: '巡更及时率',
                top: 10,
                left: '50'
              },
              axis: {
                yAxisConversion: '%', // Y轴坐标%
                xAxisDataType: 2 // X轴坐标从1月份开始
              },
              yAxis: {
                max: 100
              },
              tooltipsConfig: {
                symbol: '%',
                isNamePreposition: false,
                tipsMessage: '巡更及时率',
                tipsColor: ['#f29f46', '#b60d81']
              },
              grid: {
                top: 40
              },
              seriesData: []
            },
            legendDirection: 'level',
            legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 2 }, { name: new Date().getFullYear(), colorIndex: 1 }]
          }
        ]
      },
      // 巡更指标对比配置
      patrolOrgan: {
        borderConfig: {
          titleText: '巡更指标对比',
          moduleType: '2x1',
          ref: 'patrolOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              tipsColor: ['#f29f46', '#b60d81'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '60%'
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendClassName: true,
          legendList: [{ name: '巡更完成率', colorIndex: 2 }, { name: '巡更及时率', colorIndex: 1 }]
        },
        isShowDimension: true
      },
      // 巡更设置对比配置
      patrolSettingOrgan: {
        borderConfig: {
          titleText: '巡更设置对比',
          moduleType: '2x1',
          ref: 'patrolSettingOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: 'w',
              xAxisNameLength: 8
              // yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '%',
              tipsColor: ['#f29f46', '#b60d81', '#2e92cb'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '30%'
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendClassName: true,
          legendList: [{ name: '项目平均巡更计划', colorIndex: 2 }, { name: '项目平均巡更路线', colorIndex: 1 }]
        },
        isShowDimension: true
      }
    }
  },
  methods: {
    showTips (data) {
      this.$parent.showTips(data)
    },
    // 改变时间维度时
    changeDimension (obj) {
      switch (obj.ref) {
        case 'patrolSettingOrgan': this.getPatrolRateContrastByCompany(obj.index + 1)
          break
        case 'satisfactionOrgan': this.getPatrolSettingContrastByCompany(obj.index + 1)
          break
        default: break
      }
    },
    // 访客数量趋势配置
    getVisitorRecordContrast () {
      this.controlApi.getVisitorRecordContrast({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'amount')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.visitorRecordMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.visitorRecordMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 手机开门趋势
    getOpenDoorRecordContrast () {
      this.controlApi.getOpenDoorRecordContrast({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'amount')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.phoneOpenMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 2,
            isBar: true
          }
        ]
      }, res => {
        this.phoneOpenMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 2,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 巡更完成率
    getPatrolFinishRateContrast () {
      this.controlApi.getPatrolFinishRateContrast({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'rate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.patrolMonth.moduleConfig[0].optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
      }, res => {
        this.patrolMonth.moduleConfig[0].optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 巡更及时率
    getPatrolTimelyRateContrast () {
      this.controlApi.getPatrolTimelyRateContrast({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'rate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.patrolMonth.moduleConfig[1].optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 2
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
      }, res => {
        this.patrolOrgan.moduleConfig[1].optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 巡更指标对比
    getPatrolRateContrastByCompany (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.controlApi.getPatrolRateContrastByCompany({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let data1 = []
        let name = []
        let obj = {}
        if (res.data.code === '0') {
          obj = separateData(res.data.data, ['finishRate', 'timelyRate'])
          data = obj.data
          data1 = obj.data1
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.patrolOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.patrolOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '巡更完成率',
            colorIndex: 2,
            isBar: true
          },
          {
            data: data1,
            name: '巡更及时率',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.patrolOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.patrolOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '巡更完成率',
            colorIndex: 2,
            isBar: true
          },
          {
            data: [],
            name: '巡更及时率',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    /// 巡更设置对比
    getPatrolSettingContrastByCompany (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.controlApi.getPatrolSettingContrastByCompany({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let data1 = []
        let name = []
        let obj = {}
        if (res.data.code === '0') {
          obj = separateData(res.data.data, ['scheduleNum', 'routeNum'])
          data = obj.data
          data1 = obj.data1
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.patrolSettingOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.patrolSettingOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '项目平均巡更计划',
            colorIndex: 2,
            isBar: true
          },
          {
            data: data1,
            name: '项目平均巡更路线',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.patrolSettingOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.patrolSettingOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '项目平均巡更计划',
            colorIndex: 2,
            isBar: true
          },
          {
            data: [],
            name: '项目平均巡更路线',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请求数据
    getData () {
      this.getVisitorRecordContrast()
      this.getOpenDoorRecordContrast()
      this.getPatrolFinishRateContrast()
      this.getPatrolTimelyRateContrast()
      this.getPatrolRateContrastByCompany()
      this.getPatrolSettingContrastByCompany()
    },
    resizeChart () {
      this.$refs.module.resizeChart()
      this.$refs.module1.resizeChart()
      this.$refs.module2.resizeChart()
      this.$refs.module3.resizeChart()
      this.$refs.module4.resizeChart()
    }
  },
  mounted () {
    // this.getData()
  }
}
</script>

<style lang="scss" scoped>
  .control {
    width: calc(100% - 10px);
    position: relative;
    margin: 5px;
    overflow: hidden;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
</style>
